package metier;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import webEquipeRechercheGwt.client.entityMirror.PersonMirror;
import webEquipeRechercheGwt.client.entityMirror.TeamMirror;


@Entity
@Table(name="TEAM")
@NamedQueries(
		@NamedQuery(name="getTeams",
		query = "select t from Team t " +
				"order by t.name")
)
public class Team implements Serializable {


	private static final long serialVersionUID = 5253387106990667263L;
	private int id;
	private String name;
	private String meaning;
	private String status;
	private String urlPage;
	private Collection<Person> members;
	private TeamMirror loaded;
	
	
	@Id
	@GeneratedValue
	public int getId(){
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMeaning() {
		return meaning;
	}
	public void setMeaning(String meaning) {
		this.meaning = meaning;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String getUrlPage() {
		return urlPage;
	}
	public void setUrlPage(String urlPage) {
		this.urlPage = urlPage;
	}
	@ManyToMany
	public Collection<Person> getMembers() {
		return members;
	}
	public void setMembers(Collection<Person> members) {
		this.members = members;
	}
	public TeamMirror getMirror() {
		if(loaded==null){
			loaded=new TeamMirror();
			loaded.setId(id);
			loaded.setMeaning(meaning);
			loaded.setName(name);
			loaded.setStatus(status);
			loaded.setUrlPage(urlPage);
			Collection<PersonMirror> memberMirrors = new ArrayList<PersonMirror>();
			for(Person p : members){
				memberMirrors.add(p.getMirror());
			}
			loaded.setMembers(memberMirrors);
		}
		return loaded;
	}
	
}
